package com.firebird.app.kotlin.stock.ui.regist

import android.app.Dialog
import android.content.Intent
import android.text.TextUtils
import android.view.View
import android.widget.CheckBox
import android.widget.CompoundButton
import android.widget.EditText
import android.widget.TextView
import butterknife.BindView
import butterknife.OnCheckedChanged
import butterknife.OnClick
import com.firebird.app.kotlin.stock.base.BaseMvpActivity
import com.firebird.app.kotlin.stock.data.SimpleBean
import com.firebird.app.kotlin.stock.utils.DialogUtils.closeDialog
import com.firebird.app.kotlin.stock.utils.DialogUtils.showDialog_progressbar
import com.firebird.app.kotlin.stock.utils.ToastUtil.showToast
import com.firebird.app.kotlin.stock.Config
import com.firebird.app.kotlin.stock.R
import com.firebird.app.kotlin.stock.base.BaseView
import com.firebird.app.kotlin.stock.ui.regist.contract.RegistContract
import com.firebird.app.kotlin.stock.ui.regist.presenter.RegistPresenter
import com.firebird.app.kotlin.stock.utils.CountDownTimerUtils
import com.firebird.app.kotlin.stock.utils.DialogUtils
import com.firebird.app.kotlin.stock.utils.MaptoJsonUtils
import com.firebird.app.kotlin.stock.utils.ToastUtil


class RegistActivity : BaseMvpActivity<RegistPresenter?>(), RegistContract.View {
    @BindView(R.id.user_phone)
    var userPhone: EditText? = null
    @BindView(R.id.user_name)
    var userName: EditText? = null
    @BindView(R.id.user_pwd)
    var userPwd: EditText? = null
    @BindView(R.id.recommend)
    var recommend: EditText? = null
    @BindView(R.id.vercode)
    var vercode: EditText? = null
    @BindView(R.id.get_code)
    var getCode: TextView? = null
    @BindView(R.id.agree)
    var agree: CheckBox? = null
    @BindView(R.id.look_agree)
    var lookAgree: TextView? = null
    @BindView(R.id.bt_regist)
    var btRegist: TextView? = null
    @BindView(R.id.bt_login)
    var btLogin: TextView? = null
    private var strTel: String? = null
    private var strName: String? = null
    private var strPwd: String? = null
    private var strRecomment: String? = null
    private var strCode: String? = null
    private var dialog: Dialog? = null
    override val layoutId: Int
        get() = R.layout.activity_regist

    override fun initView() {
        mPresenter = RegistPresenter()
        mPresenter.attachView(this)
    }

    override fun showLoading() {
        dialog = showDialog_progressbar(this)
    }

    override fun hideLoading() {
        closeDialog(dialog)
    }

    override fun onError(throwable: Throwable?) {
        closeDialog(dialog)
    }

    fun onSuccess(bean: SimpleBean) {
        if (bean.respCode == Config.SUCCESS) {
            val mCountDownTimerUtils = CountDownTimerUtils(getCode, 60000, 1000)
            mCountDownTimerUtils.start()
        } else {
            showToast(this, bean.respDesc)
        }
    }

    fun onSuccessRegist(bean: SimpleBean) {
        if (bean.respCode == Config.SUCCESS) {
            showToast(this, "注册成功马上登录")
            val intent = Intent()
            intent.putExtra("tel", strTel)
            setResult(1, intent)
            finish()
        } else {
            showToast(this, bean.respDesc)
        }
    }

    @OnCheckedChanged(R.id.agree)
    fun onCheckedChanged(buttonView: CompoundButton?, isChecked: Boolean) {
        if (isChecked) { //同意协议
            btRegist!!.alpha = 1f
            btRegist!!.isEnabled = true
        } else {
            btRegist!!.alpha = 0.5f
            btRegist!!.isEnabled = false
        }
    }

    @OnClick(R.id.get_code, R.id.look_agree, R.id.bt_regist, R.id.bt_login)
    fun onViewClicked(view: View) {
        strTel = userPhone!!.text.toString().trim { it <= ' ' }
        strName = userName!!.text.toString().trim { it <= ' ' }
        strPwd = userPwd!!.text.toString().trim { it <= ' ' }
        strRecomment = recommend!!.text.toString().trim { it <= ' ' }
        strCode = vercode!!.text.toString().trim { it <= ' ' }
        when (view.id) {
            R.id.get_code ->  //获取验证码
                if (TextUtils.isEmpty(strTel) || TextUtils.isEmpty(strName) || TextUtils.isEmpty(strPwd)) {
                    showToast(this, "请完善信息")
                } else {
                    keyList.add("phone")
                    keyList.add("loginName")
                    keyList.add("loginPwd")
                    valueList.add(strTel)
                    valueList.add(strName)
                    valueList.add(strPwd)
                    mPresenter.getCode(MaptoJsonUtils.Simple(keyList, valueList))
                    keyList.clear()
                    valueList.clear()
                }
            R.id.look_agree -> {
            }
            R.id.bt_regist ->  //立即注册
                if (TextUtils.isEmpty(strTel) || TextUtils.isEmpty(strName) || TextUtils.isEmpty(strPwd) || TextUtils.isEmpty(strCode)) {
                    showToast(this, "请完善信息")
                } else {
                    keyList.add("phone")
                    keyList.add("loginName")
                    keyList.add("loginPwd")
                    keyList.add("agentCode")
                    keyList.add("yzm")
                    valueList.add(strTel)
                    valueList.add(strName)
                    valueList.add(strPwd)
                    valueList.add(strRecomment)
                    valueList.add(strCode)
                    mPresenter.getData(MaptoJsonUtils.Simple(keyList, valueList))
                    keyList.clear()
                    valueList.clear()
                }
            R.id.bt_login ->  //去登录
                finish()
        }
    }
}